package com.m3958.firstgwt.client.datasource;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.constants.CommonFieldConstants;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartConstants;
import com.m3958.firstgwt.shared.type.SmartParameterName;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;

public abstract class BaseDataSource extends RestDataSource {
	
	protected Map<String, String> defaultParameters = new HashMap<String, String>();
	
    
//    private CommonFieldConstants cconstants;
//    
//    public BaseDataSource(CommonFieldConstants cconstants){
//    	this.cconstants = cconstants;
//    }
	
	@Inject
	protected AppStatus aservice;
	
	@Inject
	protected EventBus eventBus;
	
	protected DataSourceTextField getCreatorIdsField(){
		DataSourceTextField creatorIdsField = new DataSourceTextField(SmartParameterName.CREATORIDS);
		return creatorIdsField;
	}
	
	protected DataSourceIntegerField getIdField(){
		DataSourceIntegerField idField = new DataSourceIntegerField(CommonField.ID.getValue());
		idField.setPrimaryKey(true);
		idField.setHidden(true);
		return idField;
	}
	
	protected DataSourceIntegerField getVisibleIdField(){
		DataSourceIntegerField idField = new DataSourceIntegerField(CommonField.ID.getValue());
		idField.setPrimaryKey(true);
		idField.setHidden(false);
		return idField;
	}
	
	protected DataSourceTextField getProtectLevelField(){
		DataSourceTextField plField = new DataSourceTextField(CommonField.PROTECT_LEVEL.getValue());
		return plField;
	}
	
	protected DataSourceIntegerField getParentIdHiddenField(){
		DataSourceIntegerField parentField = new DataSourceIntegerField(SmartParameterName.PARENTID,SmartParameterName.PARENTID);
        parentField.setForeignKey(id + "." + CommonField.ID.getValue());
        parentField.setRequired(true);
        parentField.setRootValue(SmartConstants.NONE_EXIST_MODEL_ID);
		parentField.setHidden(true);
		return parentField;
	}
	
	protected DataSourceIntegerField getPerpageField(CommonFieldConstants cconstants){
		DataSourceIntegerField idField = new DataSourceIntegerField(CommonField.PER_PAGE.getValue(),cconstants.perpage());
		return idField;
	}
	
	protected DataSourceBooleanField getAuditField(CommonFieldConstants cconstants){
		DataSourceBooleanField auditField = new DataSourceBooleanField(CommonField.AUDIT.getValue(),cconstants.audit());
		return auditField;
	}
	
	
	protected DataSourceIntegerField getPositionField(CommonFieldConstants cconstants){
		DataSourceIntegerField positionField = new DataSourceIntegerField(CommonField.POSITION.getValue(),cconstants.position());
		return positionField;
	}
	
	protected DataSourceIntegerField getVersionField(){
		DataSourceIntegerField versionField = new DataSourceIntegerField("version");
		versionField.setHidden(true);
		return versionField;
	}

	protected DataSourceTextField getTagNamesField(CommonFieldConstants cconstants){
		DataSourceTextField tagNamesField = new DataSourceTextField(CommonField.TAG_NAMES.getValue(),cconstants.tagnames());
		return tagNamesField;
	}

	
	protected DataSourceIntegerField getSiteIdField(CommonFieldConstants cconstants){
		DataSourceIntegerField siteIdField = new DataSourceIntegerField(CommonField.SITE_ID.getValue(), cconstants.siteId());
		siteIdField.setHidden(true);
		return siteIdField;
	}
	
	protected DataSourceDateTimeField getCreatedAtField(boolean isHidden,CommonFieldConstants cconstants){
        DataSourceDateTimeField createdAtField = new DataSourceDateTimeField(CommonField.CREATED_AT.getValue(),cconstants.createdAt());
        createdAtField.setHidden(isHidden);
        createdAtField.setCanFilter(false);
        return createdAtField;
	}
	
	protected DataSourceDateTimeField getPublishedAtField(boolean isHidden){
        DataSourceDateTimeField publisheddAtField = new DataSourceDateTimeField(CommonField.PUBLISHED_AT.getValue());
        publisheddAtField.setHidden(isHidden);
        publisheddAtField.setCanFilter(false);
        return publisheddAtField;
	}

	
//	protected DataSourceBooleanField getPublicFlagField(){
//		DataSourceBooleanField pff = new DataSourceBooleanField(CommonField.PUBLIC_FLAG.getValue(), CommonField.PUBLIC_FLAG.getCname());
//		return pff;
//	}
	
	protected DataSourceTextField getSubFetchOperationTypeField(){
		DataSourceTextField f = new DataSourceTextField(SmartParameterName.SUB_OPERATION_TYPE);
		return f;
	}
	
	protected DataSourceTextField getTplNameField(){
		DataSourceTextField tplNameField = new DataSourceTextField(CommonField.TPL_NAME.getValue(),CommonField.TPL_NAME.getValue());
		return tplNameField;
	}
 
	protected DataSourceTextField getRelationPropertyNameField(){
		DataSourceTextField f = new DataSourceTextField(SmartParameterName.RELATION_PROPERTY_NAME);
		return f;
	}
	
	protected DataSourceTextField getRelationModelIdField(){
		DataSourceTextField f = new DataSourceTextField(SmartParameterName.RELATION_MODEL_ID);
		return f;
	}
	

	public BaseDataSource(){
		setSendMetaData(true);
        setDataURL(SmartConstants.SMART_CRUD_URL);
        setDataFormat(DSDataFormat.JSON);
        addParameters();
        setDefaultParams(defaultParameters);
	}
	

	protected void addParameters(){
		defaultParameters.put(SmartParameterName.MODEL_NAME, getModelClassName());
	}
	
	protected abstract String getModelClassName();
	
	
	
	protected String getListUrlStr(String paramName, String... fields){
		String s = "";
		for (int i = 0; i < fields.length; i++) {
			s += paramName + "=" + fields[i] + "&"; 
		}
		if(s.endsWith("&"))s = s.substring(0, s.length()-1);
		return s;
	}
	
	protected String getListUrlStr(String paramName,String[]...strArrays){
		String s = "";
		for (int i = 0; i < strArrays.length; i++) {
			String[] sa = strArrays[i];
			for(int j=0; j<sa.length;j++){
			s += paramName + "=" + sa[j] + "&";
			}
		}
		if(s.endsWith("&"))s = s.substring(0, s.length()-1);
		return s;
	}
	
	public String getSimpleModelName(){
		String s= getModelClassName();
		String[] ss = s.split("\\.");
		return ss[ss.length-1];
	}
	

}
